from rest_framework.generics import ListAPIView,CreateAPIView
from rest_framework.response import Response
from meiduo_admin.serializers.user_serializers import *
from meiduo_admin.paginations import MyPage

class UserView(ListAPIView, CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserModelSerializer

    # 指定分页器
    pagination_class = MyPage

    def get_queryset(self):
        # 1、提取查询字符串参数keywork
        # 如何在非视图函数中获取请求对象？！
        # 答：self.request --> 请求对象
        keyword = self.request.query_params.get('keyword')
        if keyword:
            # 2、过滤查询集返回
            return self.queryset.filter(
                # username = "韦伟";  keywork="韦"
                # username__container=keywork 等价于  "韦" in "韦伟"
                username__contains=keyword
            )
        return self.queryset.all()